import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 以下导入的是动态路由：
// 导入封装好的路由：
import ApprovalsRouter from './modules/approvals' // 审批 路由模块
import AttendancesRouter from './modules/attendances' // 考勤 路由模块
import DepartmentsRouter from './modules/departments' // 组织架构 路由模块
import EmployeesRouter from './modules/employees' // 员工 路由模块
import PermissionRouter from './modules/permission' // 权限管理 路由模块
import SalarysRouter from './modules/salarys' // 工资 路由模块
import SettingRouter from './modules/setting' // 公司设置 路由模块
import SocialRouter from './modules/social' // 社保 路由模块

// import Import from './modules/import' // 导入excel路由模块（合并到员工模块中了）

// 所有动态路由：（需要权限显示的路由）
export const asyncRoutes = [
  DepartmentsRouter,
  SettingRouter,
  EmployeesRouter,
  PermissionRouter,
  ApprovalsRouter,
  AttendancesRouter,
  SalarysRouter,
  SocialRouter
]

// 所有静态路由：（不需要权限显示的路由）
export const constantRoutes = [{
    path: '/login',
    component: () =>
      import ('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () =>
      import ('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      // 首页
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () =>
          import ('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard', permission: 'dashboard' }
      }
    ]
  }

  // 如果访问的路由在以上规则中没有匹配到，会匹配到 *
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: [
      ...constantRoutes
      // ...asyncRoutes
    ] // 临时合并所有的路由
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
